import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_special.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/special/special_center_controller.dart';
import 'package:get/get.dart';

class SpecialListPage extends StatefulWidget {
  final int recoderType;

  SpecialListPage({Key key, @required this.recoderType = 1}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _SpecialListState();
  }
}

class _SpecialListState extends State<SpecialListPage> {
  @override
  void initState() {
    super.initState();
    Get.put(SpecialCenterController(widget.recoderType),
        tag: widget.recoderType.toString());
  }

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      onRefresh: () {
        return Get.find<SpecialCenterController>(
                tag: widget.recoderType.toString())
            .onRefresh();
      },
      child: GetBuilder(
        tag: widget.recoderType.toString(),
        id: "goodsList",
        builder: (SpecialCenterController controller) {
          bool isLoading = controller.isLoading;
          if (isLoading) {
            return SizedBox(
              width: double.infinity,
              height: Get.height / 3 * 2,
              child: Center(
                child: LoadingWidget(),
              ),
            );
          }
          List<SpecialBean> goodsList = controller.goodsList;
          if (goodsList.length == 0) {
            return SizedBox(
              width: double.infinity,
              height: Get.height / 3 * 2,
              child: Center(
                child: NoDataWidget(
                  text: "暂无积分记录 点击刷新",
                  clickCallBack: () {
                    Get.find<SpecialCenterController>(
                            tag: widget.recoderType.toString())
                        .onRefresh();
                  },
                ),
              ),
            );
          }
          return ListView.builder(
            padding: EdgeInsets.zero,
            itemCount: goodsList.length,
            itemBuilder: (BuildContext context, int index) {
              SpecialBean item = goodsList[index];
              num type = item.wartType;
              Color textColor = Colors.black;
              String preText = "";
              if (type == 1) {
                textColor = Colors.green;
                preText = "+";
              } else if (type == 2) {
                textColor = Colors.red;
                preText = "-";
              }
              if (item.totalAmount <= 0) {
                textColor = Colors.deepOrange;
                preText = "";
              }
              return GestureDetector(
                onTap: () {
                  Get.to(() => SpecialDetailsPage(item, index.toString()),
                      transition: Transition.fade,
                      duration: Duration(milliseconds: 800));
                },
                child: Hero(
                  tag: index.toString(),
                  child: Material(
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(4)),
                      ),
                      margin: EdgeInsets.only(top: 12, right: 12, left: 12),
                      padding: EdgeInsets.all(12),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            "${item.recoderTitle}",
                            style: TextStyle(fontSize: 12, color: Colors.grey),
                          ),
                          SizedBox(
                            height: 6,
                          ),
                          Row(
                            children: [
                              Text(
                                "$preText${item.totalAmount}",
                                style: TextStyle(
                                    fontSize: 16,
                                    color: textColor,
                                    fontWeight: FontWeight.w600),
                              ),
                              SizedBox(
                                width: 20,
                              ),
                              Expanded(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    Text(
                                      "培训积分 ${item.remainAmount}",
                                      textAlign: TextAlign.end,
                                      style: TextStyle(
                                          fontSize: 12, color: Colors.grey),
                                    ),
                                    SizedBox(width: 12),
                                    Text(
                                      "考核积分 ${item.examineAmount}",
                                      textAlign: TextAlign.end,
                                      style: TextStyle(
                                          fontSize: 12, color: Colors.grey),
                                    )
                                  ],
                                ),
                              ),
                            ],
                          ),
                          buildContainer(item),
                          Row(
                            children: [
                              Expanded(
                                child: Text(
                                  "创建时间 ${item.createTime}",
                                  style: TextStyle(
                                      fontSize: 12, color: Colors.grey),
                                ),
                              ),
                              SizedBox(width: 4),
                              Icon(
                                Icons.arrow_forward_ios_outlined,
                                color: Colors.grey,
                                size: 12,
                              )
                            ],
                          )
                        ],
                        mainAxisSize: MainAxisSize.min,
                      ),
                    ),
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }

  Widget buildContainer(SpecialBean item) {
    if (item.recoderContent == null || item.recoderContent.length == 0) {
      return SizedBox(
        height: 6,
      );
    }
    return Container(
      margin: EdgeInsets.only(top: 6, bottom: 6),
      padding: EdgeInsets.all(8),
      decoration: BoxDecoration(
          color: Color(0xfff0f0f0),
          borderRadius: BorderRadius.all(Radius.circular(4))),
      child: Text(
        "${item.recoderContent}",
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(fontSize: 12, color: Colors.grey),
      ),
    );
  }
}

class SpecialDetailsPage extends StatelessWidget {
  SpecialBean item;
  String tag;

  SpecialDetailsPage(this.item, this.tag);

  @override
  Widget build(BuildContext context) {
    num type = item.wartType;
    Color textColor = Colors.black;
    String preText = "";
    if (type == 1) {
      textColor = Colors.green;
      preText = "+";
    } else if (type == 2) {
      textColor = Colors.red;
      preText = "-";
    }
    if (item.totalAmount <= 0) {
      textColor = Colors.deepOrange;
      preText = "";
    }

    return Scaffold(
      backgroundColor: Color(0xfffafafa),
      appBar: AppBar(
        elevation: 1,
        title: Text("专项积分详情"),
      ),
      body: SingleChildScrollView(
        child: Container(
          width: double.infinity,
          child: Column(
            children: [
              Hero(
                tag: tag,
                child: Material(
                  child: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(Radius.circular(4)),
                    ),
                    margin: EdgeInsets.only(top: 12, right: 12, left: 12),
                    padding: EdgeInsets.all(12),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "积分 $preText${item.totalAmount}",
                          style: TextStyle(
                              fontSize: 16,
                              color: textColor,
                              fontWeight: FontWeight.w600),
                        ),
                        SizedBox(
                          height: 6,
                        ),
                        Row(
                          children: [
                            Expanded(
                              child: Text(
                                "培训积分 ${item.remainAmount}",
                                textAlign: TextAlign.start,
                                style:
                                TextStyle(fontSize: 12, color: Colors.grey),
                              ),
                            ),
                            SizedBox(
                              width: 20,
                            ),
                            Expanded(
                              child: Text(
                                "考核积分 ${item.examineAmount}",
                                textAlign: TextAlign.start,
                                style:
                                TextStyle(fontSize: 12, color: Colors.grey),
                              ),
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 6,
                        ),
                        Text(
                          "创建时间 ${item.createTime}",
                          style: TextStyle(fontSize: 12, color: Colors.grey),
                        ),
                      ],
                      mainAxisSize: MainAxisSize.min,
                    ),
                  ),
                ),
              ),
              Container(
                  width: double.infinity,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                  ),
                  margin: EdgeInsets.only(top: 12, right: 12, left: 12),
                  padding: EdgeInsets.all(12),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(item.recoderTitle),
                      SizedBox(height: 12),
                      Text(item.recoderContent),
                    ],
                  )),
            ],
          ),
        ),
      ),
    );
  }
}
